package com.ruoyi.dataAnalysisMapper.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.dataAnalysisMapper.domain.RetInfo;
import com.ruoyi.dataAnalysisMapper.mapper.MyDataAnalysisMapper;
import com.ruoyi.planthopper.domain.InvesInfo;
import com.ruoyi.planthopper.domain.QueryCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: class by cxf
 * @author: AbrahamBill
 * @date: 2022/11/30
 **/
@Slf4j
@RestController
@RequestMapping("/data")
public class DataAnaController extends BaseController {
    @Autowired
    private MyDataAnalysisMapper myDataAnalysisMapper;

    @PostMapping("/total")
    public AjaxResult getAllInsectInfo(QueryCondition queryCondition){
        List<InvesInfo> query_lst = myDataAnalysisMapper.selectInvesAllListByConditionForAna(queryCondition);
        RetInfo res = new RetInfo();
        res.initArgs();
        for(InvesInfo item:query_lst){
            res.SameDayAdd(item);
        }
        return AjaxResult.success(res);
    }

    @PostMapping("/difTime")
    public AjaxResult getAllInsectInfoByDay(QueryCondition queryCondition){
        List<InvesInfo> query_lst = new ArrayList<>();
        query_lst = myDataAnalysisMapper.selectInvesAllListByConditionForAna(queryCondition);
        List<RetInfo> date_lst = new ArrayList<>();
        if(query_lst!=null&&query_lst.size()!=0){
            String day = "";
            RetInfo current_ret = null;
            for(InvesInfo info:query_lst){
                // 如果到边界，即日期不等
                if(info.getInsertDate().equals(day)){
                    current_ret.SameDayAdd(info);
                }else{
                    if(current_ret!=null){
                        date_lst.add(current_ret);
                    }
                    current_ret = new RetInfo();
                    current_ret.initArgs();
                    current_ret.setDate(info.getInsertDate());
                    current_ret.SameDayAdd(info);
                    day = info.getInsertDate();
                }
            }
            date_lst.add(current_ret);
        }else{
            return AjaxResult.success("当前条件下不存在飞虱调查数据！");
        }
        return AjaxResult.success(date_lst);
    }

}
